package brands

import (
	"context"
	"goods_bff/controller"
	"goods_bff/forms"
	"goods_bff/goods_proto"
	"goods_bff/pkg"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func BrandList(c *gin.Context) {
	pn := c.Query("pn")
	pnInt, _ := strconv.Atoi(pn)
	pSize := c.Query("psize")
	pSizeInt, _ := strconv.Atoi(pSize)
	comment := pkg.Comment()
	rsp, err := comment.BrandList(context.Background(), &goods_proto.BrandFilterRequest{
		Pages:       int32(pnInt),
		PagePerNums: int32(pSizeInt),
	})

	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	result := make([]interface{}, 0)
	reMap := make(map[string]interface{})
	reMap["total"] = rsp.Total
	//for _, value := range rsp.Data[pnInt : pnInt*pSizeInt+pSizeInt] {
	for _, value := range rsp.Data {
		reMap := make(map[string]interface{})
		reMap["id"] = value.Id
		reMap["name"] = value.Name
		reMap["logo"] = value.Logo

		result = append(result, reMap)
	}

	reMap["data"] = result

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": reMap,
	})
}

func NewBrand(c *gin.Context) {
	brandForm := forms.BrandForm{}
	if err := c.ShouldBindJSON(&brandForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	comment := pkg.Comment()
	rsp, err := comment.CreateBrand(context.Background(), &goods_proto.BrandRequest{
		Name: brandForm.Name,
		Logo: brandForm.Logo,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	request := make(map[string]interface{})
	request["id"] = rsp.Id
	request["name"] = rsp.Name
	request["logo"] = rsp.Logo

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": request,
	})
}

func DeleteBrand(c *gin.Context) {
	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	comment := pkg.Comment()
	_, err := comment.DeleteBrand(context.Background(), &goods_proto.BrandRequest{Id: int32(idInt)})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

func UpdateBrand(c *gin.Context) {
	brandForm := forms.BrandForm{}
	if err := c.ShouldBindJSON(&brandForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}

	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	comment := pkg.Comment()
	_, err := comment.UpdateBrand(context.Background(), &goods_proto.BrandRequest{
		Id:   int32(idInt),
		Name: brandForm.Name,
		Logo: brandForm.Logo,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

func GetCategoryBrandList(c *gin.Context) {
	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	comment := pkg.Comment()
	rsp, err := comment.GetCategoryBrandList(context.Background(), &goods_proto.CategoryInfoRequest{
		Id: int32(idInt),
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	result := make([]interface{}, 0)
	for _, value := range rsp.Data {
		reMap := make(map[string]interface{})
		reMap["id"] = value.Id
		reMap["name"] = value.Name
		reMap["logo"] = value.Logo

		result = append(result, reMap)
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": result,
	})
}

func CategoryBrandList(c *gin.Context) {
	comment := pkg.Comment()
	rsp, err := comment.CategoryBrandList(context.Background(), &goods_proto.CategoryBrandFilterRequest{})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	reMap := map[string]interface{}{
		"total": rsp.Total,
	}

	result := make([]interface{}, 0)
	for _, value := range rsp.Data {
		reMap := make(map[string]interface{})
		reMap["id"] = value.Id
		reMap["category"] = map[string]interface{}{
			"id":   value.Category.Id,
			"name": value.Category.Name,
		}
		reMap["brand"] = map[string]interface{}{
			"id":   value.Brand.Id,
			"name": value.Brand.Name,
			"logo": value.Brand.Logo,
		}

		result = append(result, reMap)
	}

	reMap["data"] = result
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": reMap,
	})
}

func NewCategoryBrand(c *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	if err := c.ShouldBindJSON(&categoryBrandForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	comment := pkg.Comment()
	rsp, err := comment.CreateCategoryBrand(context.Background(), &goods_proto.CategoryBrandRequest{
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	response := make(map[string]interface{})
	response["id"] = rsp.Id

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": response,
	})
}

func UpdateCategoryBrand(c *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	if err := c.ShouldBindJSON(&categoryBrandForm); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}

	id := c.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		c.Status(http.StatusNotFound)
		return
	}
	comment := pkg.Comment()
	_, err = comment.UpdateCategoryBrand(context.Background(), &goods_proto.CategoryBrandRequest{
		Id:         int32(i),
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

func DeleteCategoryBrand(c *gin.Context) {
	id := c.Query("id")
	idInt, _ := strconv.Atoi(id)
	comment := pkg.Comment()
	_, err := comment.DeleteCategoryBrand(context.Background(), &goods_proto.CategoryBrandRequest{Id: int32(idInt)})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}
